package com.example.client.ui;

import com.example.client.socket.GameWebSocketClient1;
import lombok.Getter;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import com.example.client.game.GomokuState;

@Setter
@Getter
public class GamePanel1 extends JPanel {
    private int board_Size;      //格子数
    private int cell_Size;      //格子大小
    private int player_Size;
    private int margin;    //边距,用于坐标转换

    private  int[][] board; //0:空 1:黑 2:白
    private boolean player_black;    //ture=black回合
    private boolean GameOver;
    private boolean Forbidden_hands;

    private GameWebSocketClient1 webSocketClient; // WebSocket客户端引用

    public GamePanel1(int board_Size,int cell_Size, int player_Size,int margin) {
        super();
        this.board_Size = board_Size;
        this.cell_Size = cell_Size;
        this.player_Size = player_Size;
        this.margin = margin;

        initializeGame();

        // 添加鼠标事件监听器
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                handleMouseClick(e);
            }
        });
    }

    // 设置WebSocket客户端
    public void setWebSocketClient(GameWebSocketClient1 client) {
        this.webSocketClient = client;
    }

    // 处理鼠标点击事件
    private void handleMouseClick(MouseEvent e) {
        if (GameOver || webSocketClient == null) {
            return; // 游戏已结束或未连接服务器时不处理
        }

        // 计算点击的棋盘坐标
        int col = Math.round((e.getX() - margin) / (float) cell_Size);
        int row = Math.round((e.getY() - margin) / (float) cell_Size);

        // 检查坐标是否有效且该位置为空
        if (row >= 0 && row < board_Size && col >= 0 && col < board_Size && board[row][col] == 0) {
            // 发送下棋请求到服务器
            webSocketClient.sendMove(row, col);
        }
    }

    // 更新游戏状态
    public void updateGameState(GomokuState state) {
        this.board = state.getBoard();
        this.player_black = state.isPlayer_black();
        this.GameOver = state.isGameOver();
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 设置抗锯齿
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //棋盘
        g.setColor(new Color(220,179,92));
        g.fillRect(0,0,getWidth(),getHeight());    //实心矩形

        //网格
        g.setColor(Color.BLACK);
        for (int i = 0; i < board_Size; i++){
            //横
            g.drawLine(margin, margin + i * cell_Size,
                    margin + (board_Size - 1) * cell_Size, margin + i * cell_Size);

            g.drawLine(margin +i * cell_Size, margin,
                    margin +i * cell_Size, margin + (board_Size - 1 ) * cell_Size);
        }

        int dotSize = 8;
        int[] dots = {3, 7, 11}; // 五子棋标准的五个点位置
        for (int row : dots) {
            for (int col : dots) {
                if ((row == 7 && col == 7) || (row == 3 && col == 3) ||
                        (row == 3 && col == 11) || (row == 11 && col == 3) ||
                        (row == 11 && col == 11)) {
                    g.fillOval(margin + col * cell_Size - dotSize / 2,
                            margin + row * cell_Size - dotSize / 2,
                            dotSize, dotSize);
                }
            }
        }

        for (int row = 0; row <board_Size; row++) {
            for (int col = 0; col < board_Size; col++) {
                if (board[row][col] == 1) { // 黑子
                    g.setColor(Color.BLACK);
                    g.fillOval(margin + col * cell_Size - player_Size / 2,
                            margin + row * cell_Size - player_Size / 2,
                            player_Size,player_Size);
                } else if (board[row][col] == 2) { // 白子
                    g.setColor(Color.WHITE);
                    g.fillOval(margin + col * cell_Size - player_Size / 2,
                            margin + row * cell_Size - player_Size / 2,
                            player_Size, player_Size);
                    g.setColor(Color.BLACK);
                    g.drawOval(margin + col * cell_Size - player_Size / 2,
                            margin + row * cell_Size - player_Size / 2,
                            player_Size, player_Size);
                }
            }
        }
        // 显示当前回合
        g.setColor(Color.BLACK);

        String turn = player_black ? "黑棋回合" : "白棋回合";
        g.drawString(turn, margin, margin + board_Size * cell_Size + 30);

        // 如果游戏结束，显示游戏结束提示
        if (GameOver) {
            g.setColor(Color.RED);
            Font gameOver_Font = new Font("微软雅黑", Font.BOLD, 30);
            g.setFont(gameOver_Font);
            g.drawString("游戏结束", margin + 150, margin + board_Size * cell_Size + 30);
        }

        // 显示禁手提示
        if (Forbidden_hands) {
            g.setColor(Color.RED);
            Font forbidden_Font = new Font("微软雅黑", Font.BOLD, 20);
            g.setFont(forbidden_Font);
            g.drawString("禁手!", margin + 220, margin + board_Size * cell_Size + 30);
        }
    }
    public void initializeGame(){       //方法，初始化
        board = new int[board_Size][board_Size];
        player_black = true;
        GameOver = false;
        repaint();  //重绘
    }
}